package com.example.redissondemo.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.UUID;

/**
 * code生成器
 *
 * @author Liunh
 */
@Slf4j
public class IdGenerator {

    private static final int SHORT_UUID_BIT = 16;
    /**
     * 生成数字格式id
     */
    private static final String NUMBER_ID = "number";
    private static final Random RANDOM = new Random();
    public static String[] chars = new String[]{
            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    /**
     * 序列生成标志，number表示生成为数字，其他表示使用uuid生成
     */
    public static String single;
    /**
     * 毫秒中的增量
     */
    public static long point = 0;
    public static long pointIncr = 100000;
    /**
     * 机器节点标识
     */
    public static String node;
    private static volatile IdGenerator instance = null;

    private IdGenerator() {
    }

    public static IdGenerator getInstance() {
        if (null == instance) {
            synchronized (IdGenerator.class) {
                if (null == instance) {
                    instance = new IdGenerator();
                }
            }
        }
        return instance;
    }


    public String getUuid() {
        return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
    }

    /**
     * 获取随机字符串用于微信配置
     *
     * @return
     */
    public String getSignature() {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < SHORT_UUID_BIT; i++) {
            sb.append(chars[RANDOM.nextInt(chars.length - 1)]).append(System.currentTimeMillis());
        }
        return sb.toString();
    }

    /**
     * 生成16位uuid <功能详细描述>
     *
     * @return
     */
    public String generateShortUuid(String single, String node) {
        final StringBuffer shortBuffer = new StringBuffer();
        if (null == node || "".equals(node)) {
            log.error(">>>>>> 机器节点错误");
        }
        //生成数字格式
        if (NUMBER_ID.equals(single)) {
            shortBuffer.append(node).append(getSequenceNumber());
            return shortBuffer.toString();
        }
        final String uuid = getUuid();
        String str;
        int x;
        for (int i = 0; i < SHORT_UUID_BIT; i++) {
            str = uuid.substring(i * 2, i * 2 + 2);
            x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    public String getSequenceNumber(String node) {
        final StringBuffer shortBuffer = new StringBuffer();
        if (null == node || "".equals(node)) {
            log.error("机器节点错误");
        }
        shortBuffer.append(node).append(getSequenceNumber());
        return shortBuffer.toString();
    }

    /**
     * 采用同步方法,获取毫秒时间+增量值
     *
     * @return
     */
    public synchronized String getSequenceNumber() {
        point++;
        if (point == pointIncr) {
            point = 1;
        }
        return System.currentTimeMillis() + String.valueOf(point);
    }


}
